package com.ccp.dev.form.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.model.FormDef;
import com.ccp.dev.form.model.FormField;
import com.ccp.dev.form.model.FormRule;
import com.ccp.dev.form.service.FormDefService;
import com.ccp.dev.form.service.FormFieldService;
import com.ccp.dev.form.service.FormRuleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 自定义查询控制器类
 *
 * @author chenjing
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_FORM + "/formRule")
@Action(ownermodel = SysAuditModelType.FORM_MANAGEMENT)
public class FormRuleController extends SysBaseController {

    @Resource
    private FormRuleService formRuleService;

    @Resource
    private FormDefService formDefService;

    @Resource
    private FormFieldService formFieldService;

    /**
     * 数据列表页面跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/list")
    @Action(description = "表单验证规则数据列表页面跳转",exectype = "页面跳转日志")
    public String formQueryViewList() {
        return getAutoView();
    }

    /**
     * 新增及编辑页面跳转
     * @param request 请求
     * @param modelMap modelMap
     * @return 跳转路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/add")
    @Action(description = "表单验证规则新增及编辑页面跳转",exectype = "页面跳转日志")
    public String formQueryViewAdd(HttpServletRequest request, ModelMap modelMap) {
        String ruleId = RequestUtil.getString(request,"ruleId");
        modelMap.put("ruleId",ruleId);
        return getAutoView();
    }

    /**
     * 明细页面跳转
     * @param request 请求
     * @param modelMap modelMap
     * @return 跳转路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/detail")
    @Action(description = "表单验证规则明细页面跳转",exectype = "页面跳转日志")
    public String formQueryViewPreview(HttpServletRequest request, ModelMap modelMap) {
        String ruleId = RequestUtil.getString(request,"ruleId");
        modelMap.put("ruleId",ruleId);
        return getAutoView();
    }

    /**
     * 取得表单验证规则的分页列表
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description = "查询表单验证规则分页列表",exectype = "查询日志",detail = "查询表单验证规则分页列表<#if success>成功<#else>失败</#if>")
    public ResultData list(HttpServletRequest request) {
        QueryFilter filter = new QueryFilter(request, true);
        try {
            List<FormRule> formRuleList = formRuleService.queryList(filter);
            SysAuditThreadLocalHolder.putParamerter("success",true);
            return getResultDataSuccess("query.success", filter.getPageBean().getTotalCount(), formRuleList);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return getResultDataError("query.failure");
        }
    }

    /**
     * 表单验证规则明细
     * @param request 请求
     * @return ResultData
     */
    @RequestMapping("/detail")
    @ResponseBody
    @Action(description = "查询表单验证规则明细",exectype = "查询日志",detail = "查询表单验证规则明细<#if success>成功<#else>失败</#if>")
    public ResultData detail(HttpServletRequest request){
            String ruleId = RequestUtil.getString(request,"ruleId");
            try {
                FormRule formRule = formRuleService.getOneById(ruleId);
                SysAuditThreadLocalHolder.putParamerter("success",true);
                return getResultDataSuccess("query.success", formRule);
            }catch (Exception e){
                SysAuditThreadLocalHolder.putParamerter("success",false);
                return getResultDataError("query.failure");
            }
    }

    /**
     * 表单验证规则添加
     *
     * @param formRule 验证规则对象
     * @return ResultData
     */
    @RequestMapping("/add")
    @ResponseBody
    @Action(description = "添加或更新表单验证规则",detail = "添加与更新表单验证规则<#if success>成功<#else>失败</#if>")
    public ResultData add(@RequestBody FormRule formRule){
            try {
                if(BeanUtils.isEmpty(formRule.getId())){
                    String alias = formRule.getAlias();
                    if(formRuleService.queryForAlias(alias)){
                        return getResultDataError("别名已存在");
                    }
                }
                formRuleService.save(formRule);
                SysAuditThreadLocalHolder.putParamerter("success",true);
                return getResultDataSuccess("save.success");
            }catch (Exception e){
                e.printStackTrace();
                SysAuditThreadLocalHolder.putParamerter("success",false);
                return getResultDataError("save.failure");
            }
    }

    /**
     * 删除表单验证规则
     *
     * @param request 请求
     * @return ResultData
     */
    @RequestMapping("/del")
    @ResponseBody
    @Action(description = "删除表单验证规则",detail = "删除表单验证规则<#if success>成功<#else>失败</#if>")
    public ResultData del(HttpServletRequest request){
        try {
            String[] ids = RequestUtil.getStringAryByStr(request, "ruleIds");
            formRuleService.delByIds(ids);
            SysAuditThreadLocalHolder.putParamerter("success",true);
            return getResultDataSuccess("delete.success");
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return getResultDataError("delete.failure");
        }
    }

    /**
     * 根据表单 获取 对应表单验证规则数据
     *
     * @param request 请求
     * @return ResultData
     */
    @RequestMapping("/getRuleByFormKey")
    @ResponseBody
    @Action(description = "根据表单获取验证规则数据",detail = "查询日志<#if success>成功<#else>失败</#if>")
    public ResultData getRuleByFormKey(HttpServletRequest request){
        String formKey = RequestUtil.getString(request, "formKey");
        try {
            FormDef formDef = formDefService.getDefaultVersionByFormKey(formKey);
            List<FormField> formFieldList = formFieldService.getByTableId(formDef.getTableId());
            Set<String> set = new HashSet<>(16);
            for(FormField formField : formFieldList){
                String validRule = formField.getValidRule();
                if(StringUtil.isNotEmpty(validRule)) {
                    set.add(validRule);
                }
            }
            List<FormRule> formRuleList = formRuleService.getRulesBySetAlias(set);
            SysAuditThreadLocalHolder.putParamerter("success",true);
            return getResultDataSuccess("query.success",formRuleList);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return getResultDataError("query.failure");
        }
    }
}
